package com.account.book.service.impl;

import com.account.book.entity.SuggestInfo;
import com.account.book.enums.DataStatusEnum;
import com.account.book.mapper.SuggestInfoMapper;
import com.account.book.request.SuggestInfoReqVo;
import com.account.book.response.SuggestInfoResVo;
import com.account.book.service.SuggestInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SuggestInfoServiceImpl extends ServiceImpl<SuggestInfoMapper, SuggestInfo> implements SuggestInfoService {
    private static final Logger logger = LoggerFactory.getLogger(SuggestInfoServiceImpl.class);
    @Autowired
    private MapperFacade mapperFacade;

    /**
     * @param @param suggestInfoReqVo
     * @param userId
     * @return
     * @author liang-hw
     * @description 新增或者更新建议信息
     * @date 2024/01/15 14:33:31
     */
    @Override
    public void addAndUpdateSuggestInfo(SuggestInfoReqVo suggestInfoReqVo, String userId) {
        SuggestInfo suggestInfo = mapperFacade.map(suggestInfoReqVo, SuggestInfo.class);
        //新增建议
        if (StringUtils.isEmpty(suggestInfoReqVo.getId())) {
            suggestInfo.setUserId(userId);
            suggestInfo.setStatus(DataStatusEnum.ENABLE.getValue());
            this.save(suggestInfo);
        } else {
            //更新
            UpdateWrapper<SuggestInfo> suggestInfoUpdateWrapper = new UpdateWrapper<>();
            suggestInfoUpdateWrapper.lambda()
                    .eq(SuggestInfo::getId, suggestInfoReqVo.getId())
                    .eq(SuggestInfo::getStatus, DataStatusEnum.ENABLE.getValue());
            this.update(suggestInfo, suggestInfoUpdateWrapper);
        }
    }

    /**
     * @param @param userId
     * @return @return {@link List }<{@link SuggestInfoResVo }>
     * @author liang-hw
     * @description 查询建议信息
     * @date 2024/01/15 14:42:49
     */
    @Override
    public List<SuggestInfoResVo> selectSuggestInfo(String userId) {
        QueryWrapper<SuggestInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SuggestInfo::getUserId, userId)
                .eq(SuggestInfo::getStatus, DataStatusEnum.ENABLE.getValue())
                .orderByDesc(SuggestInfo::getCreateTime);
        List<SuggestInfo> suggestInfos = this.list(queryWrapper);
        List<SuggestInfoResVo> suggestInfoResVoList = mapperFacade.mapAsList(suggestInfos, SuggestInfoResVo.class);
        return suggestInfoResVoList;
    }

    /**
     * @param @param suggestInfoReqVo
     * @return
     * @author liang-hw
     * @description 删除建议信息
     * @date 2024/01/15 14:48:54
     */
    @Override
    public void deleteSuggestInfo(SuggestInfoReqVo suggestInfoReqVo) {
        UpdateWrapper<SuggestInfo> suggestInfoUpdateWrapper = new UpdateWrapper<>();
        suggestInfoUpdateWrapper.lambda()
                .eq(SuggestInfo::getId, suggestInfoReqVo.getId())
                .set(SuggestInfo::getStatus, DataStatusEnum.DELETE.getValue());
        this.update(null, suggestInfoUpdateWrapper);
    }
}
